<?php

namespace App\Requests\Admin\GoodsClass;


use App\Enums\GoodsClassStatusEnum;
use App\Requests\BaseRequest;
use App\Traits\ValidatorTrait;
use Illuminate\Validation\Rule;

class GoodsClassIndexRequest extends BaseRequest
{
    use ValidatorTrait;

    /**
     * 字段注解
     *
     * @return array
     */
    public function attributes(): array
    {
        return [
            'id' => 'ID',
            'name' => '名称',
            'begin_time' => '开始时间',
            'end_time' => '开始时间',
            'status' => '状态',
        ];
    }

    public function rules(): array
    {
        return [
            // nullable 允许参数包含null, 空字符串
            'id' => ['sometimes', 'nullable', 'integer'],
            'name' => ['sometimes', 'nullable', 'string'],
            'status' => ['sometimes', 'nullable', Rule::in(GoodsClassStatusEnum::getKeyList()),],
            'begin_time' => ['sometimes', 'nullable', 'date'],
            'end_time' => ['sometimes', 'nullable', 'date', function ($attribute, $value, $fail) {
                $beginTime = $this->input('begin_time');
                if (!$beginTime || !$value) {
                    return;
                }
                $beginTimeTimestamp = strtotime($beginTime);
                $endTimeTimestamp = strtotime($value);
                if ($endTimeTimestamp <= $beginTimeTimestamp) {
                    $fail('结束时间必须大于开始时间');
                }
            }],
        ];
    }
}
